package dao

import javax.inject.Inject

import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

class LiteratureDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  def deleteAll: Future[Unit] = db.run(Literature.delete).map(_ => ())

  def insertAll(rows: Seq[LiteratureRow]): Future[Unit] = {
    db.run(Literature ++= rows).map(_ => ())
  }

  def selectAll: Future[Seq[LiteratureRow]] = db.run(Literature.result)

  def selectByTitle(title: String): Future[LiteratureRow] = db.run(Literature.filter(_.entitle === title).result.head)

  def selectById(id: Int): Future[LiteratureRow] = db.run(Literature.filter(_.id === id).result.head)

  def update(row: LiteratureRow): Future[Unit] = db.run(Literature.filter(_.id === row.id).update(row)).map(_ => ())


}
